﻿using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Lor;
using ExpenseSystem.Common;
using System.Collections.Generic;
using System.Web;
using System;

namespace ExpenseSystem.Fether
{
    public abstract class NormalFetcher<T> where T:NormalModel,new()
    {
        protected int rows;
        protected string tran = HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString();
        protected UserProfile userProfile = HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] as UserProfile;
        public virtual T Get( long site, long id, out string message)
        {
            Restrain restrainId = Restrain.Eq("AutoId", id).And(Restrain.Eq("Avaliable", true)).And(Restrain.Eq("Site",site));
            message = null;
            IList<T> result = SqlProvider.GetDbInstance<T>().Select(tran, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, restrainId);
            if (result.Count > 0)
                return result[0];
            return null;
        }
        public virtual IList<T> List( long site, IList<OrderBy> orderBy, Limit limit, out string message,out int rows, params Restrain[] restrains)
        {
            IList<Restrain> restrainList = new List<Restrain>(restrains);
            restrainList.Add(Restrain.Eq("Avaliable", true).And(Restrain.Eq("Site",site)));
            message = null;
            Restrain[] param = new Restrain[restrainList.Count];
            restrainList.CopyTo(param, 0);
            IList<T> result = SqlProvider.GetDbInstance<T>().Select(tran, orderBy, limit, out message,out rows, param);
            return result;
        }

        public virtual IList<T> List(long site, int state, IList<OrderBy> orderBy, Limit limit, out string message,out int rows, params Restrain[] restrains)
        {
            IList<Restrain> restrainList = new List<Restrain>(restrains);
            restrainList.Add(Restrain.Eq("Avaliable", true).And(Restrain.Eq("Site", site)).And(Restrain.Eq("State",state)));
            message = null;
            Restrain[] param = new Restrain[restrainList.Count];
            restrainList.CopyTo(param, 0);
            IList<T> result = SqlProvider.GetDbInstance<T>().Select(tran, orderBy, limit, out message,out rows, param);
            return result;
        }
    }
}
